package com.carpark.ejb.DAO;

import com.carpark.ejb.Model.CarsModel;
import com.carpark.ejb.Model.ClientsModel;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Piotr
 */
@Stateful
public class CarDAOImpl implements CarDAO {

    private EntityManager entityManager;

    public CarDAOImpl() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistenceUnit");
        entityManager = factory.createEntityManager();
    }

    @Override
    public CarsModel getCarById(Integer id) {
        return (CarsModel) entityManager.createNamedQuery("CarsModel.findByCarId").setParameter("carId", id).getSingleResult();
    }

    public CarsModel getCarByModel(String model) {
        return (CarsModel) entityManager.createNamedQuery("CarsModel.findByModel").setParameter("model", model).getSingleResult();
    }

    @Override
    public EntityManager getEntityManager() {
        return this.entityManager;
    }

    @Override
    public CarsModel addCar(CarsModel cr) {
        entityManager.getTransaction().begin();
        entityManager.persist(cr);
        entityManager.getTransaction().commit();
        return cr;
    }

    @Override
    public int getCarsSize() {
        return entityManager.createNamedQuery("CarsModel.findAll").getResultList().size();
    }

    @Override
    public void removeCar(Integer id) {
        CarsModel rowToDelete = getCarById(id);
        entityManager.getTransaction().begin();
        entityManager.remove(rowToDelete);
        entityManager.getTransaction().commit();
    }

    @Override
    public void updateCar(CarsModel cr) {
        try {
            CarsModel actual = getCarById(cr.getCarId());
            entityManager.getTransaction().begin();
            entityManager.persist(actual);
            actual.setNumberPlate(cr.getNumberPlate());
            actual.setMake(cr.getMake());
            actual.setModel(cr.getModel());
            actual.setComments(cr.getComments());
            entityManager.getTransaction().commit();
        }
        catch(Throwable t) {
            updateCar(cr);
        }
    }

    @Override
    public List<Vector<String>> findAll() {
        List<CarsModel> cars = entityManager.createNamedQuery("CarsModel.findAll").getResultList();
        List<Vector<String>> carsInString = new ArrayList<Vector<String>>();
        for (CarsModel car : cars) {
            carsInString.add(car.makeStringList());
        }
        return carsInString;
    }

    @Override
    public List<CarsModel> getAllCars() {
        return entityManager.createNamedQuery("CarsModel.findAll").getResultList();
    }

    @Override
    public List<CarsModel> getCarsByClient(ClientsModel cl) {
        return (List<CarsModel>) entityManager.createNamedQuery("CarsModel.findByClientId").setParameter("clientId", cl).getResultList();
    }

    @Override
    public List<CarsModel> getCarsByClientAndType(ClientsModel cl, String type) {
        return (List<CarsModel>) entityManager.createNamedQuery("CarsModel.findByClientIdAndType").setParameter("clientId", cl).setParameter("type", type).getResultList();
    }
}
